package org.hoyi.dispatchs;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.hoyi.DB.ctrl.Console;
import org.hoyi.disptachs.model.JsonModel;

import net.sf.json.JSONSerializer;

/**
 * Author:ellen Email:ellen@kuaifish.com CreateDate:2015-05-20
 * ModifyDate:2015-06-20 hoyi entities @ hoyi.org 使用请在项目关于内标注hoyi版权，
 * hoyi版权归hoyi.org所有
 */
public class responseUtil {
	private static responseUtil _instance;

	public static responseUtil getinstance() {
		if (_instance == null) {
			_instance = new responseUtil();
		}
		return _instance;
	}

	public void Redirect(String directurl, HttpServletResponse response) {
		try {
			response.sendRedirect(directurl);
		} catch (IOException e) {
	    	Console.Error(e);
		}
	}

	public void Write(HttpServletResponse response, String content, String contenttype) throws IOException {
		response.setContentType(contenttype);

		PrintWriter out = response.getWriter();

		out.println(content);
		out.flush();
		out.close();
	}
	
//	public void Write(HttpServletResponse response, byte[] content, String contenttype) throws IOException {
//		response.setContentType(contenttype);
//
//		PrintWriter out = response.getWriter();
//
//		out.println(content);
//		out.flush();
//		out.close();
//	}

	public void WriteUTF8Text(HttpServletResponse response, String content) throws IOException {
		Write(response, content, "text; charset=UTF-8");
	}

	public void WriteUTF8HTML(HttpServletResponse response, String content) throws IOException {
		Write(response, content, "text/html; charset=UTF-8");
	}
	
	public void WriteUTF8CSS(HttpServletResponse response, String content) throws IOException{
		Write(response, content, "text/css; charset=UTF-8");
	}

	public void WriteUTF8Plain(HttpServletResponse response, String content) throws IOException{
		Write(response, content, "text/plain; charset=UTF-8");
	}

	public void WriteUTF8JSON(HttpServletResponse response, String content) throws IOException {
		Write(response, content, "text/json; charset=UTF-8");
	}

	public void WriteUTF8JSON(HttpServletResponse response, JsonModel jsonModel) throws IOException {
		Write(response, JSONSerializer.toJSON(jsonModel).toString(), "text/json; charset=UTF-8");
	}
}
